package org.urchr.beorganized.api.impl.user;

import org.postgresql.util.Base64;
import org.urchr.beorganized.api.base.BaseAPIServlet;
import org.urchr.beorganized.api.base.BaseServlet;
import org.urchr.beorganized.api.base.Log;
import org.urchr.beorganized.dao.MD5;
import org.urchr.beorganized.dao.UsersDAO;
import org.urchr.beorganized.dao.XMLDocumentBuilder;
import org.urchr.beorganized.dao.errors.GeneralException;
import org.urchr.beorganized.dao.tables.User;

public class CheckUserNamePassword extends BaseAPIServlet {
	
	public CheckUserNamePassword() {
		skipBody = true;
	}
	private static final long serialVersionUID = -8310656835048869572L;

	@Override
	protected void doProcess() throws GeneralException {
		User user = UsersDAO.
			authorizeUser(request.getParameter("userName"), request.getParameter("password"), true);
		user = UsersDAO.updateUserTimeZone(user);
		XMLDocumentBuilder doc = new XMLDocumentBuilder();
		UsersDAO.userToNode(doc, doc.getRootElement("user"), user);
		String string = user.getLogin()+"@"+user.getPassword();
		Log.debug(this, "User "+user.getLogin()+" entered BeOrganized! (Via login)");
		doc.getRootElement().setAttribute("token", Base64.encodeBytes(string.getBytes()));
		sendNode(doc.getRootElement());
	}

}
